
********************************************************************************
********************************************************************************
*************************** THIS IS DO FILE 6 (MCO/FFS)*************************
************************* THIS FILE RUNS THE LIKELIHOOD ************************
********************************************************************************
********************************************************************************

*Version: 2023 March 29


cap mkdir `"$output/ffs_mco_separation/model_estimation"'
cap mkdir `"$output/ffs_mco_separation/log/6_model_estimation"'

	
cap log close 
log using `"$output/ffs_mco_separation/log/6_model_estimation/6_model_estimation.smcl"', replace 

di "This run uses code version from 2023/03/29"

use  `"$output/ffs_mco_separation/temp/estimation_data_bin.dta"', clear


//set criteria for observations to be included in the liklihood
bys pay_type_ffs_mco state visit_code_cat: gen cell_obs = _N 
gen no_resub_line = 0
replace no_resub_line = 1 if choice_S == 1 & n_S == . 
bys visit_billing_id: gegen no_resub = max(no_resub_line)
bys pay_type_ffs_mco state visit_code_cat: gegen cell_sd =  sd(no_resub)
replace cell_sd = 0 if cell_sd == . 
gen valid_cell = 0 
replace valid_cell = 1 if cell_obs >= 10 & cell_sd > 0 

gen visit_code_cat_n = 1 if visit_code_cat == "administrative"
replace visit_code_cat_n = 2 if visit_code_cat == "contractual"
replace visit_code_cat_n = 3 if visit_code_cat == "coverage"
replace visit_code_cat_n = 4 if visit_code_cat == "duplicate"
replace visit_code_cat_n = 5 if visit_code_cat == "information"



** continuation value terms **
gen continuation = .99*V_S+.99*0.5772156649
replace continuation = 0 if n_S ==.



** constraint on coefficient of continuation value **
constraint 1 continuation = 1

gen line_cost = .
gen line_cost_se = .
gen claim_cost = .
gen claim_cost_se = .

gen line_cost_adm = . 
gen line_cost_adm_se = . 
gen line_cost_cont = . 
gen line_cost_cont_se = . 
gen line_cost_cov = . 
gen line_cost_cov_se = . 
gen line_cost_dup = . 
gen line_cost_dup_se = .  
gen line_cost_info = . 
gen line_cost_info_se = . 

gen claim_cost_adm = . 
gen claim_cost_adm_se = . 
gen claim_cost_cont = . 
gen claim_cost_cont_se = . 
gen claim_cost_cov = . 
gen claim_cost_cov_se = . 
gen claim_cost_dup = . 
gen claim_cost_dup_se = . 
gen claim_cost_info = . 
gen claim_cost_info_se = . 

label var line_cost "Line Parameter"
label var line_cost_se "Line Parameter SE"
label var claim_cost "Claim Parameter "
label var claim_cost_se "Claim Parameter SE"

label var line_cost_adm "Line Parameter Administrative"
label var line_cost_adm_se "Line Parameter Administrative SE"
label var claim_cost_adm "Claim Parameter Administrative"
label var claim_cost_adm_se "Claim Parameter Administrative SE"

label var line_cost_cont "Line Parameter Contractual Adjustment"
label var line_cost_cont_se "Line Parameter Contractual Adjustment SE"
label var claim_cost_cont "Claim Parameter Contractual Adjustment"
label var claim_cost_cont_se "Claim Parameter Contractual Adjustment SE"

label var line_cost_cov "Line Parameter Coverage"
label var line_cost_cov_se "Line Parameter Coverage SE"
label var claim_cost_cov "Claim Parameter Coverage"
label var claim_cost_cov_se "Claim Parameter Coverage SE"

label var line_cost_dup "Line Parameter Duplicate"
label var line_cost_dup_se "Line Parameter Duplicate SE"
label var claim_cost_dup "Claim Parameter Duplicate"
label var claim_cost_dup_se "Claim Parameter Duplicate SE"

label var line_cost_info "Line Parameter Information"
label var line_cost_info_se "Line Parameter Information SE"
label var claim_cost_info "Claim Parameter Information"
label var claim_cost_info_se "Claim Parameter Information SE"


cap xi, noomit  : gen i.state

forv s = 1/51 {
	
	cap replace _Istate_`s' = 0 if n_S == .

}
	
gen extra_line = (-n_S+1)
replace extra_line = 0 if n_S == .



forvalues size = 1/2 {
	
	if `size' == 1 {
		local grup_size "small"
	}
	
	if `size' == 2 {
		local grup_size "large"
	}
	

	forvalues x = 1/4 {
		
		if `x' == 1 {
			local payer "Medicare"
		}
		
		else if `x' == 2 {
			local payer "Medicaid_FFS"
		}
		
		else if `x' == 3 {
			local payer "Medicaid_MCO"
		}
		
		else if `x' == 4 {
			local payer "Commercial"
		}
		
		forvalues r = 1/5 {
		
			if `r' == 1 {
				local reason "Administrative"
			}
			
			else if `r' == 2 {
				local reason "contractual"
			}
			
			else if `r' == 3 {
				local reason "coverage"
			}
		
			else if `r' == 4 {
				local reason "duplicate"
			}
			
			
			else if `r' == 5 {
				local reason "information"
			}
		
		
			** MLE - heterogeneity by payer and reason code and reason code-specific constant resubmission cost 
			di `"These are the estimates for `payer' for reason code category `reason' and group size `grup_size'"'
			clogit choice_S _Istate* extra_line continuation if pay_type_ffs_mco ==`x' & visit_code_cat_n == `r' & size_indicator == `size', constraint(1) group(visit_billing_id) iter($iterations)


			cap replace line_cost     =  _b[extra_line]   if pay_type_ffs_mco == `x'    &    visit_code_cat_n == `r' & size_indicator == `size'
			cap replace line_cost_se  =  _se[extra_line]  if pay_type_ffs_mco == `x' &    visit_code_cat_n == `r' & size_indicator == `size'

			
			forv s = 1/54 {
				cap replace claim_cost	= _b[_Istate_`s'] if state == `s'    & pay_type_ffs_mco == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
				cap replace claim_cost_se = _se[_Istate_`s'] if state == `s'  & pay_type_ffs_mco == `x' &    visit_code_cat_n == `r' & size_indicator == `size'
			}


		
		
			if (`r' == 1) {
			
				cap replace line_cost_adm = _b[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				cap replace line_cost_adm_se = _se[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_adm = _b[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_adm_se = _se[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 2) {
			
				cap replace line_cost_cont = _b[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				cap replace line_cost_cont_se = _se[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_cont = _b[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cont_se = _se[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					
				}
			}
			
			else if (`r' == 3) {
			
				cap replace line_cost_cov = _b[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				cap replace line_cost_cov_se = _se[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_cov = _b[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_cov_se = _se[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 4) {
			
				cap replace line_cost_dup = _b[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				cap replace line_cost_dup_se = _se[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'

				forv s = 1/54 {
				
					cap replace claim_cost_dup = _b[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_dup_se = _se[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
			else if (`r' == 5) {
			
				cap replace line_cost_info = _b[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				cap replace line_cost_info_se = _se[extra_line] if pay_type_ffs_mco == `x' & size_indicator == `size'
				
				forv s = 1/54 {
				
					cap replace claim_cost_info = _b[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
					cap replace claim_cost_info_se = _se[_Istate_`s'] if pay_type_ffs_mco == `x' & state == `s' & size_indicator == `size'
				
				}
			}
			
		}		
	
	}

}

* keep relevant observations
bys pay_type_ffs_mco state visit_code_cat size_indicator: keep if _n == 1
keep pay_type_ffs_mco state line_cost* claim_cost* payee_state_cd visit_code_cat visit_code_cat_n size_indicator
compress

save "$output/ffs_mco_separation/model_estimation/estimations_bin.dta", replace 

keep if pay_type_ffs_mco == 2 | pay_type_ffs_mco == 3
bys pay_type_ffs_mco state size_indicator: keep if _n == 1
cap drop line_cost line_cost_se claim_cost claim_cost_se visit_code_cat visit_code_cat_n

save "$output/ffs_mco_separation/model_estimation/estimations_medicaid_bin.dta", replace 


log close


